package gen1us.jutw.CreateConfig;

import java.util.ArrayList;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;

/*
 Copyright 2009 Evgeny Yusov

 This file is part of JUTW.

 JUTW is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 JUTW is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with JUTW.  If not, see http://www.gnu.org/licenses/.
 */

class Config {
	public void addParam(String param, String label) {
		addParam(param, label, REQUIRED);
	}

	public void addParam(String param, String label, String defValue) {
		properties.add(new Property(param, label, false, defValue));
		types.add(new Type(TEXTFIELD, properties.size() - 1));
	}

	public void addParam(String param, String label, int isRequired) {
		Property tmpProperty = new Property(param, label);
		properties.add(tmpProperty);
		tmpProperty.setRequired(isRequired);
		types.add(new Type(TEXTFIELD, properties.size() - 1));
	}

	public void addParamPass(String param, String label, int isRequired) {
		Property tmpProperty = new Property(param, label, true);
		properties.add(tmpProperty);
		tmpProperty.setRequired(isRequired);
		types.add(new Type(PASSFIELD, properties.size() - 1));
	}
	
	public void addParamPass(String param, String label){
		addParamPass(param, label, REQUIRED);
	}

	public void addComboBox(ComboBox cBox) {
		comboBoxes.add(cBox);
		types.add(new Type(COMBOBOX, comboBoxes.size() - 1));
	}

	public void addCheckBox(String param, String desc) {
		checkBoxes.add(new CheckBox(param, desc, 1));
		types.add(new Type(CHECKBOX, checkBoxes.size() - 1));
	}

	public void setParamValue(int tid, String s) {
		int type = types.get(tid).getType();
		int index = types.get(tid).getIndex();
		if (type == TEXTFIELD || type == PASSFIELD) {
			properties.get(index).setValue(s);
		} else if (type == CHECKBOX) {
			checkBoxes.get(index).setValue(s);
		}
	}

	public void setCheckBoxValue(int tid, Boolean v) {
		int type = getType(tid);
		int index = getIndex(tid);
		if (type == CHECKBOX) {
			if (v)
				checkBoxes.get(index).setValue("yes");
			else
				checkBoxes.get(index).setValue("");
		}
	}

	public void addCut() {
		cutLine = types.size();
	}

	public int getCut() {
		return cutLine;
	}

	public Boolean getCheckBoxValue(int tid) {
		int type = types.get(tid).getType();
		int index = types.get(tid).getIndex();
		if (type == CHECKBOX) {
			return checkBoxes.get(index).getValue() == "yes";
		}
		return null;
	}

	public void saveToFile() {
		try {
			FileWriter wrt = new FileWriter(new File("info.user"));
			for (Property p : properties) {
				if (p.isRequired() == REQUIRED)
					wrt.write("--" + p.getParam() + "=" + p.getValue() + "\n");
				else if(!p.getValue().equals(""))
					wrt.write("--" + p.getParam() + "=" + p.getValue() + "\n");
			}
			for (CheckBox c : checkBoxes) {
				if (c.getWriter() == 1) {
					if (c.getValue() == "yes")
						wrt.write("--" + c.getParam() + "=" + c.getValue()
								+ "\n");
				} else if (c.getWriter() == 2)
					if (c.getValue() == "yes")
						wrt.write("--" + c.getParam() + "\n");
			}
			wrt.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	int getParamTid(String s) {
		for (int pid = 0; pid < types.size(); pid++) {
			int type = types.get(pid).getType();
			int index = types.get(pid).getIndex();
			if (type == TEXTFIELD || type == PASSFIELD) {
				Property p = properties.get(index);
				if (p.getParam().equals(s))
					return pid;
			} else if (type == CHECKBOX) {
				CheckBox c = checkBoxes.get(index);
				if (c.getParam().equals(s))
					return pid;
			}
		}
		return -1;
	}

	public String getValue(int tid) {
		if (tid >= 0) {
			int type = types.get(tid).getType();
			int index = types.get(tid).getIndex();
			if (type == TEXTFIELD || type == PASSFIELD) {
				return properties.get(index).getValue();
			} else if (type == CHECKBOX) {
				return checkBoxes.get(index).getValue();
			}
		} else
			System.err.println("Can't get value. Check your config");
		return null;
	}

	public String[] getComboBoxItems(int tid) {
		int type = types.get(tid).getType();
		int index = types.get(tid).getIndex();
		if (type == COMBOBOX) {
			ComboBox cBox = comboBoxes.get(index);
			return cBox.getItems();
		}
		return null;
	}

	public int[] getChangedParamsId(int tid, String s) {
		int type = getType(tid);
		int index = getIndex(tid);
		if (type == COMBOBOX) {
			ComboBox cBox = comboBoxes.get(index);
			int size = cBox.getNumberParams(s);
			int[] paramsTid = new int[size];
			for (int j = 0; j < size; j++) {
				String param = cBox.getParam(s, j);
				paramsTid[j] = getParamTid(param);
			}
			return paramsTid;
		}
		return null;
	}

	public void selectMenuItem(int tid, String s) {
		int type = getType(tid);
		int index = getIndex(tid);
		if (type == COMBOBOX) {
			int numberParams;

			// if (lastSelected != null) {
			// numberParams = lastSelected.getNumberParams(s);
			// for (int j = 0; j < numberParams; j++) {
			// String param = lastSelected.getParam(s, j);
			// String value = lastSelected.getValue(s, j);
			// int id = findParam(param);
			// if (id >= 0)
			// setParamValue(id, value);
			// }
			// }

			ComboBox cBox = comboBoxes.get(index);

			// lastSelected = new ComboBox("");
			cBox.clearLI();
			numberParams = cBox.getNumberParams(s);
			for (int j = 0; j < numberParams; j++) {
				String param = cBox.getParam(s, j);
				String value = cBox.getValue(s, j);
				int tidParam = findParam(param);
				if (tidParam >= 0)
					cBox.addLIParam(param, getValue(tidParam));
				setParamValue(tidParam, value);
			}
			// cBox.setLastItem(s);
		}
	}

	public void enableLI(int tid) {
		int type = getType(tid);
		int index = getIndex(tid);
		if (type == COMBOBOX) {
			ComboBox cBox = comboBoxes.get(index);
			int numberParams;
			numberParams = cBox.getLINumberParams();
			if (numberParams > 0)
				for (int j = 0; j < numberParams; j++) {
					String param = cBox.getLIParam(j);
					String value = cBox.getLIValue(j);
					int id = findParam(param);
					if (id >= 0)
						setParamValue(id, value);
				}
		}
	}

	int findParam(String s) {
		for (int tid = 0; tid < types.size(); tid++) {
			int type = getType(tid);
			int index = getIndex(tid);
			if (type == TEXTFIELD) {
				if (properties.get(index).getParam().equals(s))
					return tid;
			} else if (type == CHECKBOX) {
				if (checkBoxes.get(index).getParam().equals(s))
					return tid;
			}
		}
		return -1;
	}

	public int size() {
		return types.size();
	}

	public Boolean isProperty(int tid) {
		return getType(tid) == TEXTFIELD;
	}

	public Boolean isPropertyPass(int tid) {
		return getType(tid) == PASSFIELD;
	}

	public Boolean isComboBox(int tid) {
		return getType(tid) == COMBOBOX;
	}

	public Boolean isCheckBox(int tid) {
		return getType(tid) == CHECKBOX;
	}

	public int getType(int tid) {
		return types.get(tid).getType();
	}

	public int getIndex(int tid) {
		return types.get(tid).getIndex();
	}

	public String getDesc(int tid) {
		int type = getType(tid);
		int index = getIndex(tid);
		if (type == TEXTFIELD || type == PASSFIELD)
			return properties.get(index).getDesc();
		else if (type == COMBOBOX)
			return comboBoxes.get(index).getDesc();
		else if (type == 4)
			return checkBoxes.get(index).getDesc();
		return null;
	}

	private int cutLine;
	private final ArrayList<CheckBox> checkBoxes = new ArrayList<CheckBox>();
	private final ArrayList<Property> properties = new ArrayList<Property>();
	private final ArrayList<ComboBox> comboBoxes = new ArrayList<ComboBox>();
	private final ArrayList<Type> types = new ArrayList<Type>();
	static final int OPTIONAL = Property.OPTIONAL;
	static final int REQUIRED = Property.REQUIRED;
	static final int TEXTFIELD = 1;
	static final int PASSFIELD = 2;
	static final int COMBOBOX = 3;
	static final int CHECKBOX = 4;

	private class Type {
		public Type(int type, int index) {
			this.type = type;
			this.index = index;
		}

		public int getType() {
			return type;
		}

		public int getIndex() {
			return index;
		}

		private final int index;
		private final int type;
	}

	public void addCheckBoxWithoutYes(String param, String desc) {
		checkBoxes.add(new CheckBox(param, desc, 2));
		types.add(new Type(CHECKBOX, checkBoxes.size() - 1));
	}

	public int[] getChangedParamsLIId(int tid) {
		int type = getType(tid);
		int index = getIndex(tid);
		if (type == COMBOBOX) {
			ComboBox cBox = comboBoxes.get(index);
			int size = cBox.getLINumberParams();
			int[] paramsTid = new int[size];
			for (int j = 0; j < size; j++) {
				String param = cBox.getLIParam(j);
				paramsTid[j] = getParamTid(param);
			}
			return paramsTid;
		}
		return null;
	}

}
